﻿using MediatR;
using SmartMedicalCare.API.Read.Applications.Commands;
using SmartMedicalCare.Domain;
using SmartMedicalCare.ErronCode;
using SmartMedicalCare.Infrastructure.Implaction;

namespace SmartMedicalCare.API.Read.Applications.CommandHandlers
{
    public class LoginCommandHandler : IRequestHandler<LoginCommand, APIResult<UserModel>>
    {
        private readonly IBaseRespository<UserModel> userRep;

        public LoginCommandHandler(IBaseRespository<UserModel> userRep)
        {
            this.userRep = userRep;
        }

        public Task<APIResult<UserModel>> Handle(LoginCommand request, CancellationToken cancellationToken)
        {
            APIResult<UserModel> result=new APIResult<UserModel>();
            result.code=ResultCode.显示成功;
            result.Message=ResultCode.显示成功.ToString();

            var res = from a in userRep.GetAll()
                      where a.UserName == request.UserName && a.UserPass == request.UserPass
                      select new UserModel
                      {
                          UserId = a.UserId,
                          UserName = a.UserName,
                          UserNickName = a.UserNickName,
                          UserPass = a.UserPass,
                      };
            var tt=res.FirstOrDefault();
            if (tt == null)
            {
                result.code = ResultCode.显示失败;
                result.Message = ResultCode.显示失败.ToString()+"用户名或密码错误";
                return Task.FromResult(result);
            }

            result.Message = ResultCode.显示成功.ToString()+"登陆成功";
            result.Data= tt;
            return Task.FromResult(result);
        
        
        }
    }
}
